package com.administrator.scoreManageSys.controller.course;

import com.administrator.scoreManageSys.service.ClassMangeService;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.administrator.scoreManageSys.system.SystemConstant.CURRENT_SYSTEM_USER;

/**
 * 班级管理
 *
 * @author zsh
 * @date 2022/03/23/ 9:50
 */
@RestController
@RequestMapping(value = "/class/manage")
public class ClassMangeController {


    @Resource
    private ClassMangeService classMangeService;

    /**
     * 获取班级列表
     *
     * @return 列表
     */
    @PostMapping(value = "/classList")
    public JSONObject classList(@RequestBody JSONObject data) {
        try {
            Integer pageNum = data.getInteger("pageNum");
            Integer pageSize = data.getInteger("pageSize");
            String name = data.getString("name");
            return classMangeService.classList(pageNum, pageSize, name);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取班级列表 异常");
        }
    }

    /**
     * 添加班级
     *
     * @return 列表
     */
    @PostMapping(value = "/classAdd")
    @RequiresRoles("education")
    @RequiresPermissions(value = "class:add", logical = Logical.OR)
    public JSONObject classAdd(@RequestBody JSONObject data, HttpServletRequest request) {
        try {
            //新添班级名称
            String name = data.getString("name");

            //添加班级的班主任
            Long classTeacher = data.getLong("classTeacher");
            //操作人id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            return classMangeService.classAdd(name, classTeacher, userId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加班级 异常");
        }
    }

    /**
     * 删除班级
     *
     * @return 列表
     */
    @PostMapping(value = "/classDelete")
    @RequiresRoles("education")
    @RequiresPermissions(value = "class:add", logical = Logical.OR)
    public JSONObject classDelete(@RequestBody JSONObject data) {
        try {
            Long id = data.getLong("id");
            return classMangeService.classDelete(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除班级 异常");
        }
    }

    /**
     * 修改班级
     *
     * @return 列表
     */
    @PostMapping(value = "/classUpdate")
    @RequiresRoles("education")
    @RequiresPermissions(value = "class:add", logical = Logical.OR)
    public JSONObject classUpdate(@RequestBody JSONObject data) {
        try {
            //修改的班级id
            Long id = data.getLong("id");
            //修改班级名称
            String name = data.getString("name");
            //修改班级的班主任
            Long classTeacher = data.getLong("classTeacher");
            return classMangeService.classUpdate(name, id, classTeacher);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改班级 异常");
        }
    }

    /**
     * 获取所有教师列表
     *
     * @return 列表
     */
    @PostMapping(value = "/classTeacherList")
    @RequiresRoles("education")
    @RequiresPermissions(value = "class:add", logical = Logical.OR)
    public JSONObject classTeacherList(@RequestBody JSONObject data) {
        try {
            return classMangeService.classTeacherList();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取所有教师列表 异常");
        }
    }

    /**
     * 根据班级获取班科目
     *
     * @return 列表
     */
    @PostMapping(value = "/classSubjectList")
    @RequiresRoles(value = {"education"})
    @RequiresPermissions(value = {"class:add","examination:add"}, logical = Logical.OR)
    public JSONObject classSubjectList(@RequestBody JSONObject data) {
        try {
            Integer classId = data.getInteger("classId");
            return classMangeService.classSubjectList(classId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("根据班级获取班科目 异常");
        }
    }
}
